<script lang="ts" setup name="XtxCity">
export type AreaListObj = {
  code: string;
  level: number;
  name: string;
  areaList: AreaListObj[];
};

export type CityResult = {
  provinceCode: string;
  provinceName: string;
  cityCode: string;
  cityName: string;
  countyCode: string;
  countyName: string;
};

import { ref, watch } from "vue";
import { onClickOutside } from "@vueuse/core";
import axios from "axios";

const active = ref(false);
const toggle = () => {
  active.value = !active.value;
};
const target = ref(null);

const cityLit = ref<AreaListObj[]>([]);
const cacheList = ref<AreaListObj[]>([]);
const getCityList = async () => {
  const res = await axios.get<AreaListObj[]>(
    "https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json"
  );
  cityLit.value = res.data;
  cacheList.value = res.data;
};

// 选择城市
const changeResult = ref({
  provinceCode: "",
  provinceName: "",
  cityCode: "",
  cityName: "",
  countyCode: "",
  countyName: "",
});
const emit = defineEmits<{ (e: "changeCity", value: CityResult): void }>();
const selectCity = (item: AreaListObj) => {
  console.log("city", item);
  if (item.level === 0) {
    changeResult.value.provinceCode = item.code;
    changeResult.value.provinceName = item.name;
    cityLit.value = item.areaList;
  }
  if (item.level === 1) {
    changeResult.value.cityCode = item.code;
    changeResult.value.cityName = item.name;
    cityLit.value = item.areaList;
  }
  if (item.level === 2) {
    changeResult.value.countyCode = item.code;
    changeResult.value.countyName = item.name;
    active.value = false;
    // 子传父，往父组件传递选择的地址信息数据
    emit("changeCity", changeResult.value);
  }
};
getCityList();

// 监听弹框的关闭，只要弹框关了，就需要重置数据
// 这里一进入页面不需要立刻执行一次，正常用watch(watchEffect一般就是用于处理路由缓存的问题)
watch(active, (value) => {
  if (!value) {
    cityLit.value = cacheList.value;
  }
});
onClickOutside(target, () => {
  active.value = false;
});
defineProps<{
  userAddress: string;
}>();
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{ active: active }">
      <span class="value" v-if="userAddress">{{ userAddress }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span
        class="ellipsis"
        v-for="item in cityLit"
        :key="item.code"
        @click="selectCity(item)"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;

  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;

    &.active {
      background: #fff;
    }

    .placeholder {
      color: #999;
    }

    .value {
      color: #666;
      font-size: 12px;
    }

    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }

  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;

    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;

      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
